package com.winter.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.winter.mapper.DictEntryMapper;
import com.winter.mapper.IenterpriseFinancingMapper;
import com.winter.mapper.UserInfoMapper;
import com.winter.model.DictEntry;
import com.winter.model.IenterpriseFinancing;
import com.winter.model.UserInfo;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

@Service
public class IenterpriseFinancingService {

    @Resource
    public IenterpriseFinancingMapper ienterpriseFinancingMapper;
    @Resource
    public DictEntryMapper dictEntryMapper;
    @Resource
    public UserInfoMapper userInfoMapper;

    public Result getIenterList(Integer pageNumber, Integer pageSize, String proStatus, String dcFinancingType, String finDeadline, String expectAnnualYear, String finAmoutn, String proRating, String userNo, String key) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        /*if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }*/
        /*if (Strings.isNullOrEmpty(proStatus)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择产品状态");
        }
        if (Strings.isNullOrEmpty(dcFinancingType)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择产品类型");
        }
        if (Strings.isNullOrEmpty(proRating)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择债项评级");
        }
        if (Strings.isNullOrEmpty(expectAnnualYear)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择利率");
        }
        if (Strings.isNullOrEmpty(finAmoutn)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择金额");
        }
        if (Strings.isNullOrEmpty(finDeadline)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择期限");
        }*/
        String finDeadlineShow = null;
        if ("" != finDeadline && null != finDeadline) {
            DictEntry ty2 = dictEntryMapper.get("CAPITAL_MATURITY", finDeadline);
            if (null != ty2) {
                if (ty2.getDictname()
                       .equals("5年以上")) {
                    finDeadline = "60";
                    finDeadlineShow = "600";
                } else if (ty2.getDictname()
                              .equals("3-5年")) {
                    finDeadline = "36";
                    finDeadlineShow = "60";
                } else if (ty2.getDictname()
                              .equals("1-3年")) {
                    finDeadline = "12";
                    finDeadlineShow = "36";
                } else if (ty2.getDictname()
                              .equals("6-12月")) {
                    finDeadline = "6";
                    finDeadlineShow = "12";
                } else if (ty2.getDictname()
                              .equals("6月以下")) {
                    finDeadline = "0";
                    finDeadlineShow = "6";
                }
            }
        }
        String finAmoutnShow = null;
        if ("" != finAmoutn && null != finAmoutn) {
            DictEntry ty3 = dictEntryMapper.get("DC_MONEY_QUERY", finAmoutn);
            if (null != ty3) {
                if (ty3.getDictname()
                       .equals("5000万以下")) {
                    finAmoutn = "0";
                    finAmoutnShow = "5000";
                } else if (ty3.getDictname()
                              .equals("5000万-1亿")) {
                    finAmoutn = "5000";
                    finAmoutnShow = "10000";
                } else if (ty3.getDictname()
                              .equals("1亿-3亿")) {
                    finAmoutn = "10000";
                    finAmoutnShow = "30000";
                } else if (ty3.getDictname()
                              .equals("3亿-5亿")) {
                    finAmoutn = "30000";
                    finAmoutnShow = "50000";
                } else if (ty3.getDictname()
                              .equals("5亿以上")) {
                    finAmoutn = "50000";
                    finAmoutnShow = "1000000";
                }
            }
        }


        String expectannualHight = null;
        if ("" != expectAnnualYear && null != expectAnnualYear) {
            DictEntry ty4 = dictEntryMapper.get("CAPITAL_INTEREST_RATE", expectAnnualYear);
            if (null != ty4) {
                if (ty4.getDictname()
                       .equals("3%以下")) {
                    expectAnnualYear = "0";
                    expectannualHight = "3";
                } else if (ty4.getDictname()
                              .equals("3%~5%")) {
                    expectAnnualYear = "3";
                    expectannualHight = "5";
                } else if (ty4.getDictname()
                              .equals("5%~8%")) {
                    expectAnnualYear = "5";
                    expectannualHight = "8";
                } else if (ty4.getDictname()
                              .equals("8%以上")) {
                    expectAnnualYear = "8";
                    expectannualHight = "100";
                }
            }
        }

        List<IenterpriseFinancing> finOnlineList = ienterpriseFinancingMapper.getIenterList(proStatus, dcFinancingType, finDeadline, finDeadlineShow, expectAnnualYear, expectannualHight, finAmoutn, finAmoutnShow, proRating, key);
        PageInfo<IenterpriseFinancing> pageInfo = new PageInfo<>(finOnlineList);
        return Result.success(pageInfo);
    }

    /**
     * 通过id获取资金平台_债券信息
     */
    @SuppressWarnings("unused")
	public Result getIenterBean(Long id,String userNo) {
    	if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
    	UserInfo uit=userInfoMapper.selectByUserNo(userNo);
    	if(null!=uit){
    		if(uit.getAuditFlg().equals("0")){
    			return Result.failure(ResultCode.PARAM_IS_BLANK, "该会员未认证,无详情查看权限");
    		}
    	}
        IenterpriseFinancing ient = ienterpriseFinancingMapper.selectByPrimaryKey(id);
        if (null != ient) {
        	if(uit.getIsvip().equals("1")){
        		if (null != ient.getCreateUser()) {
                    UserInfo ui = userInfoMapper.selectByUserNo(ient.getCreateUser());
                    if (null != ui) {
                        if (null != ui.getCustTel()) {
                            ient.setUserTel(ui.getCustTel());
                        }
                        if (null != ui.getCustMobile()) {
                            ient.setUserMobile(ui.getCustMobile());
                        }
                        if (null != ui.getCustEmail()) {
                            ient.setUserEmail(ui.getCustEmail());
                        }
                    }
                }
        	}
            

            DictEntry ty = dictEntryMapper.get("DC_FIN_MONEY_UNIT", ient.getFinAmountUnit());
            if (null != ty) {
        		/*if(ty.getDictname().equals("万")){
        			ient.setFinAmountUnit("万");
        		}else if(ty.getDictname().equals("亿")){
        			ient.setFinAmountUnit("亿");
        		}*/
                ient.setFinAmountUnit(ty.getDictname());
            }

            DictEntry ty2 = dictEntryMapper.get("DC_FIN_DATE_UNIT", ient.getFinDeadlineUnit());
            if (null != ty2) {
        		/*if(ty2.getDictname().equals("月")){
        			ient.setFinDeadlineUnit("月");
        		}else if(ty2.getDictname().equals("年")){
        			ient.setFinDeadlineUnit("年");
        		}else if(ty2.getDictname().equals("日")){
        			ient.setFinDeadlineUnit("日");
        		}*/
                ient.setFinDeadlineUnit(ty2.getDictname());
            }

            if (null != ient.getStateOwnFlg()) {
                if (ient.getStateOwnFlg()
                        .equals("1")) {
                    ient.setStateOwnFlg("是");
                } else if (ient.getStateOwnFlg()
                               .equals("0")) {
                    ient.setStateOwnFlg("否");
                }
            }
            if (null != ient.getAssureFlg()) {
                if (ient.getAssureFlg()
                        .equals("1")) {
                    ient.setAssureFlg("是");
                } else if (ient.getAssureFlg()
                               .equals("0")) {
                    ient.setAssureFlg("无");
                }
            }

            DictEntry ty3 = dictEntryMapper.get("DC_FINANCING_TYPE", ient.getDcFinancingType());
            DictEntry ty4 = dictEntryMapper.get("DC_FINANCING_TYPE_CURRENCY", ient.getSubclass());
            if (null != ty3) {
                ient.setDcFinancingType(ty3.getDictname());
            } else if (null != ty4) {
                ient.setDcFinancingType(ty4.getDictname());
            } else if (null != ty3 && null != ty4) {
            	ient.setDcFinancingType(ty3.getDictname() + "-" + ty4.getDictname());
            }
            return Result.success(ient);
        }
        return null;
    }

    /**
     * 新增资金平台信息
     *
     * @return int
     */
    public Result insertIenter(IenterpriseFinancing ien) {
        if (null != ien) {
            if (Strings.isNullOrEmpty(ien.getCreateUser())) {
                return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
            }
            UserInfo uit=userInfoMapper.selectByUserNo(ien.getCreateUser());
        	if(null!=uit){
        		if(uit.getAuditFlg().equals("0")){
        			return Result.failure(ResultCode.PARAM_IS_BLANK, "该会员未认证,无详情查看权限");
        		}
        	}
            if (Strings.isNullOrEmpty(ien.getDcFinancingType())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择产品类型");
            }
            if (Strings.isNullOrEmpty(ien.getSubclass())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择产品子项类型");
            }
            if (Strings.isNullOrEmpty(ien.getProName())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入产品名称");
            }
            if (Strings.isNullOrEmpty(ien.getFinOwner())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资主体");
            }
            if (Strings.isNullOrEmpty(ien.getFinUsefor())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资用途");
            }
            if (0 >= ien.getFinAmoutn()) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资金额");
            } else {
                DictEntry ty = dictEntryMapper.get("DC_FIN_MONEY_UNIT", ien.getFinAmountUnit());
                if (null != ty) {
                    if (ty.getDictname()
                          .equals("万")) {
                        ien.setFinAmountShow(ien.getFinAmoutn());
                    } else if (ty.getDictname()
                                 .equals("亿")) {
                        ien.setFinAmoutn(ien.getFinAmoutn() * 10000);
                        ien.setFinAmountShow(ien.getFinAmoutn());
                    }
                }
            }
            if (0 >= ien.getFinDeadline()) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资期限");
            } else {
                DictEntry ty = dictEntryMapper.get("DC_FIN_DATE_UNIT", ien.getFinDeadlineUnit());
                if (null != ty) {
                    if (ty.getDictname()
                          .equals("月")) {
                        //ien.setFinDeadline(ien.getFinDeadline());
                        ien.setFinDeadlineShow(ien.getFinDeadline());
                    } else if (ty.getDictname()
                                 .equals("年")) {
                        ien.setFinDeadline(ien.getFinDeadline() * 12);
                        ien.setFinDeadlineShow(ien.getFinDeadline());
                    } else if (ty.getDictname()
                                 .equals("日")) {
                        //ien.setFinDeadline(ien.getFinDeadline());
                        ien.setFinDeadlineShow(ien.getFinDeadline());
                    }
                }
            }
            if (ien.getExpectAnnualYearLow() == null || ien.getExpectAnnualYearHigh() == null) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入利率");
            } else {
                if (ien.getExpectAnnualYearLow() != "" && ien.getExpectAnnualYearHigh() != "") {
                    ien.setExpectAnnualYear(ien.getExpectAnnualYearLow() + "~" + ien.getExpectAnnualYearHigh());
                } else if (ien.getExpectAnnualYearLow() == "" && ien.getExpectAnnualYearHigh() != "") {
                    ien.setExpectAnnualYear(ien.getExpectAnnualYearHigh());
                    ien.setExpectAnnualYearLow(ien.getExpectAnnualYearHigh());
                } else if (ien.getExpectAnnualYearLow() != "" && ien.getExpectAnnualYearHigh() == "") {
                    ien.setExpectAnnualYear(ien.getExpectAnnualYearLow());
                    ien.setExpectAnnualYearHigh(ien.getExpectAnnualYearLow());
                }

            }
            if (Strings.isNullOrEmpty(ien.getProRating())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "债项评级不能为空");
            }
            if (Strings.isNullOrEmpty(ien.getMainRating())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "主体评级不能为空");
            }
       /*     if (Strings.isNullOrEmpty(ien.getAssureType())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入担保方式");
            }*/
            ien.setUpdateUser(ien.getCreateUser());
            ien.setCreatetime(new Date());
            ien.setUpdatetime(new Date());
            ien.setIsvalid("1");
            ien.setAuditing("0");//0.审核中，1完成，2.失败
            ien.setProType("1");//1.债券；2.非标。
            ien.setProStatus("1");
            int i = ienterpriseFinancingMapper.insertSelective(ien);
            if (i > 0) {
                return Result.success("添加成功");
            } else {
                return Result.failure(ResultCode.INSERT_ERROR);
            }
        } else {
            return Result.success("添加参数为空");
        }
    	/*int i=ienterpriseFinancingMapper.insert(ien);
    	if(i>0){
    		return Result.success("新增成功");
    	}else{
    		return Result.failure(ResultCode.INSERT_ERROR);
    	}*/
    }

    /**
     * 更新资金平台债券信息
     *
     * @return int
     */
    public Result updateIenter(IenterpriseFinancing ien) {
        if (null != ien) {
            if (Strings.isNullOrEmpty(ien.getCreateUser())) {
                return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
            }
            if (ien.getId() <= 0) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "产品id不能为空");
            }
            if (Strings.isNullOrEmpty(ien.getDcFinancingType())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择产品类型");
            }
            if (Strings.isNullOrEmpty(ien.getSubclass())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择产品子项类型");
            }
            if (Strings.isNullOrEmpty(ien.getProName())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入产品名称");
            }
            if (Strings.isNullOrEmpty(ien.getFinOwner())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资主体");
            }
            if (Strings.isNullOrEmpty(ien.getFinUsefor())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资用途");
            }
            if (0 >= ien.getFinAmoutn()) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资金额");
            } else {
                DictEntry ty = dictEntryMapper.get("DC_FIN_MONEY_UNIT", ien.getFinAmountUnit());
                if (null != ty) {
                    if (ty.getDictname()
                          .equals("万")) {
                        ien.setFinAmountShow(ien.getFinAmoutn());
                    } else if (ty.getDictname()
                                 .equals("亿")) {
                        ien.setFinAmoutn(ien.getFinAmoutn() * 10000);
                        ien.setFinAmountShow(ien.getFinAmoutn());
                    }
                }
            }
            if (0 >= ien.getFinDeadline()) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入融资期限");
            } else {
                DictEntry ty = dictEntryMapper.get("DC_FIN_DATE_UNIT", ien.getFinDeadlineUnit());
                if (null != ty) {
                    if (ty.getDictname()
                          .equals("月")) {
                        //ien.setFinDeadline(ien.getFinDeadline());
                        ien.setFinDeadlineShow(ien.getFinDeadline());
                    } else if (ty.getDictname()
                                 .equals("年")) {
                        ien.setFinDeadline(ien.getFinDeadline() * 12);
                        ien.setFinDeadlineShow(ien.getFinDeadline());
                    } else if (ty.getDictname()
                                 .equals("日")) {
                        //ien.setFinDeadline(ien.getFinDeadline());
                        ien.setFinDeadlineShow(ien.getFinDeadline());
                    }
                }
            }
            if (ien.getExpectAnnualYearLow() == null || ien.getExpectAnnualYearHigh() == null) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入利率");
            } else {
                if (ien.getExpectAnnualYearLow() != "" && ien.getExpectAnnualYearHigh() != "") {
                    ien.setExpectAnnualYear(ien.getExpectAnnualYearLow() + "~" + ien.getExpectAnnualYearHigh());
                } else if (ien.getExpectAnnualYearLow() == "" && ien.getExpectAnnualYearHigh() != "") {
                    ien.setExpectAnnualYear(ien.getExpectAnnualYearHigh());
                    ien.setExpectAnnualYearLow(ien.getExpectAnnualYearHigh());
                } else if (ien.getExpectAnnualYearLow() != "" && ien.getExpectAnnualYearHigh() == "") {
                    ien.setExpectAnnualYear(ien.getExpectAnnualYearLow());
                    ien.setExpectAnnualYearHigh(ien.getExpectAnnualYearLow());
                }

            }
            if (Strings.isNullOrEmpty(ien.getProRating())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "债项评级不能为空");
            }
            if (Strings.isNullOrEmpty(ien.getMainRating())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "主体评级不能为空");
            }
       /*     if (Strings.isNullOrEmpty(ien.getAssureType())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入担保方式");
            }*/

            ien.setUpdateUser(ien.getCreateUser());
            //ien.setCreatetime(new Date());
            ien.setUpdatetime(new Date());
            //ien.setIsvalid("1");
            //ien.setAuditing("0");//0.审核中，1完成，2.失败
            // ien.setProType("1");//1.债券；2.非标。
            int i = ienterpriseFinancingMapper.updateByPrimaryKeySelective(ien);
            if (i > 0) {
                return Result.success("更新成功");
            } else {
                return Result.failure(ResultCode.INSERT_ERROR);
            }
        } else {
            return Result.success("更新参数为空");
        }

    }

    public static void main(String[] args) {
        String t = "5年";
        String s = "20月";
        String d = t.substring(0, t.length() - 1);
        String d2 = t.substring(t.length() - 1, t.length());
        String y = s.substring(0, s.length() - 1);
        System.out.println(d2 + "-------" + y);
    }
}
